package com.hzx.zhihu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzx.zhihu.domain.po.Result;
import com.hzx.zhihu.domain.po.User;
import com.hzx.zhihu.mapper.UserMapper;
import com.hzx.zhihu.service.UserService;
import com.hzx.zhihu.utils.Md5Util;
import com.hzx.zhihu.utils.WeChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Result<User> login(String username, String password) {
        // 参数校验
        if (username == null || password == null || username.trim().isEmpty() || password.trim().isEmpty()) {
            return Result.failure(400, "用户名或密码不能为空");
        }

        // 查询用户信息
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName, username));
        if (user == null) {
            return Result.failure(404, "用户名不存在");
        }

        // 检查密码是否匹配
        if (!Objects.equals(user.getPassword(), password)) {
            return Result.failure(401, "密码错误");
        }

        // 登录成功，返回用户信息
        return Result.success(user);
    }

}
